const Controller = require('../lib/controller');
const pub = require('../boot/redis').redisPublisher;
const redis = require('../boot/redis').redisClient;
const util = require('../lib/util');

class BasicController extends Controller {
    async onSend(inputs) {
        let res = await redis.hget('session', inputs.sendTo);
        let session = JSON.parse(res);
        let msg = ['receiveMessage', {
                sendFrom: {
                    id: this.session.user.id,
                    name: this.session.user.name,
                },
                content: inputs.content,
                sendTo: inputs.sendTo,
                timestamp: util.timestamp()
            }
        ];
        pub.publish(session.processCode, JSON.stringify(msg));
    }
}

module.exports = function(socket, processCode, session) {
    return new BasicController(socket, processCode, session);
};